package com.tianyalei.wolf.sample.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.tianyalei.wolf.sample.R;

import java.util.Random;

/**
 * Created by wolf on 2016/4/7.
 */
public abstract class BaseDialog extends Dialog {
    protected Context mContext;
    /**
     * 对话框的布局
     */
    protected View mDialogView;

    public BaseDialog(Context context) {
        this(context, R.style.base_dialog);
    }

    public BaseDialog(Context context, int style) {
        super(context, style);
        this.mContext = context;
        mDialogView = View.inflate(context, setLayoutView(), null);
        this.setContentView(mDialogView);
        setOwnerActivity((Activity) context);

        init(mDialogView);
    }

    @Override
    public void show() {
        Window dialogWindow = getWindow();
        // 获取屏幕宽、高用
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (display.getWidth() * 0.65); // 宽度设置为屏幕的0.65

        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);

        super.show();
    }

    /**
     * 带特效动画的显示
     *
     * @param type
     */
    public void show(Effectstype type) {
        BaseEffects animator = type.getAnimator();
        animator.setDuration(700);
        animator.start(mDialogView);
        show();
    }

    /**
     * 显示随机动画
     */
    public void showRandomAnim() {
        Effectstype type = null;
        int i = new Random(System.currentTimeMillis()).nextInt(13);
        switch (Math.abs(i)) {
            case 0:
                type = Effectstype.Fadein;
                break;
            case 1:
                type = Effectstype.Fall;
                break;
            case 2:
                type = Effectstype.Fliph;
                break;
            case 3:
                type = Effectstype.Flipv;
                break;
            case 4:
                type = Effectstype.Newspager;
                break;
            case 5:
                type = Effectstype.RotateBottom;
                break;
            case 6:
                type = Effectstype.RotateLeft;
                break;
            case 7:
                type = Effectstype.Sidefill;
                break;
            case 8:
                type = Effectstype.SlideBottom;
                break;
            case 9:
                type = Effectstype.Slideleft;
                break;
            case 10:
                type = Effectstype.Slideright;
                break;
            case 11:
                type = Effectstype.Slidetop;
                break;
            case 12:
                type = Effectstype.Slit;
                break;
            default:
                break;
        }

        show(type);
    }

    /**
     * 设置用哪个布局
     */
    protected abstract int setLayoutView();

    protected abstract void init(View mDialogView);
}
